<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {ajax} from "@/js/api_tool.js";
import {iframe} from "@/js/tool.js";
import {_config, decode_config, s94_event} from "@/js/s94_tool.js";
import {S94Model} from "@/js/S94Model.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let model_name = this.$get('model_name','');
    let model_name_config = decode_config(_config('model_map'), {content_key:'label', value_key:'name'});
    let forms_config = {
      title: {label:'名称',name:'title',type:'text_one',tips:'建议长度1-20'},
      model_name: {label:'模型', name:'model_name', type:'select_one',config:model_name_config,value:model_name},
      parent_id: {label:'父级类目',name:'parent_id',type:'list_id',config:model_name?'/category/index?model_name='+model_name:'',tips:'不选为顶级类目',value: 0},
      head_img: {label:'头图',name:'head_img',type:'file_img',tips:''},
      info: {label:'描述',name:'info',type:'text_more',tips:'描述，长度0-150'},
      legal_help_template: {label:'法务服务模板',name:'extend[legal_help_template]',type:'text_more',tips:'由Blade模板编译，语法规则参考：https://learnku.com/docs/laravel/6.x/blade/5147',remove:true},
      sort: {label:'排序',name:'sort',type:'text_one',tips:'排序，从大到小',value:50},
      is_show: {label:'是否显示',name:'is_show',type:'radio_switch', config:['否','是'],tips:'',value:1},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,

      model_map: {},
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/category/edit?id=`+this.id : '/admin/category/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        s94_event(`category/${this.id?'edit':'add'}`, res);
        if (this.__iframe){
          // iframe.exe('category/index', '$refs.list_url.list');
          iframe.hide('category/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    category_extend_param(model_name){
      let model = S94Model.find(model_name);
      if (!model) return;
      let has = s94Mjs.to_string(model.category_extend_param).split(",");
      let map = ["legal_help_template"]
      map.forEach((k)=>{
        this.config[k].remove = !~has.indexOf(k);
      })
    }
  },
  watch:{
    'config.model_name.value'(v){
      this.config.parent_id.config = '/category/index?model_name='+v;
      this.category_extend_param(v);
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/category/info?id='+this.id, cache:true}, (res)=>{
        this.config.parent_id.config = '/category/index?model_name='+res.data.model_name;
        try {
          let extend = JSON.parse(res.data["extend"]);
          Object.keys(extend||[]).forEach((k) => {
            res.data[`extend[${k}]`] = extend[k];
          })
        } catch (e) {}
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('category/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    if (this.$get('model_name')){
      app.config.model_name.type = "hidden";
    }
  }
}
</script>
